package com.imm.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.NullArgumentException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


/**
 * 手机验证码发送工具类
 */
@Component
@Slf4j
public class SMSSendService {

    @Value("${reggie.sms.regionId}")
    private String regionId;

    @Value("${reggie.sms.accessKeyId}")
    private String accessKeyId;

    @Value("${reggie.sms.secretAccessKey}")
    private String secretAccessKey;

    @Value("${reggie.sms.action}")
    private String action;

    @Value("${reggie.sms.domain}")
    private String domain;

    @Value("${reggie.sms.signName}")
    private String signName;

    @Value("${reggie.sms.templateCode}")
    private String templateCode;

    @Value("${reggie.sms.version}")
    private String version;

    public void sendCode(String phone, String code) {
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(code)) {
            log.error("参数异常");
            return;
        }
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, secretAccessKey);
        DefaultAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        // 请求方法
        request.setSysMethod(MethodType.POST);
        // 请求域名
        request.setSysDomain(domain);
        // 请求版本
        request.setSysVersion(version);
        // 请求动作
        request.setSysAction(action);
        // 手机号码
        request.putQueryParameter("PhoneNumbers", phone);
        // 签名
        request.putQueryParameter("SignName", signName);
        // 模板
        request.putQueryParameter("TemplateCode", templateCode);
        // 验证码
        request.putQueryParameter("TemplateParam", "{ \"code\":\"" + code + "\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            log.info("信息发送成功，验证码为：{}", code);
        } catch (ClientException e) {
            throw new RuntimeException(e);
        }

    }


}
